using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Reflection;
using System.Resources;
using System.IO;
using System.Threading;
using System.Drawing.Design;
using System.Drawing.Imaging;
using System.Web.UI.Design;
using System.Globalization;

namespace MultilanguageControls
{
    /// <summary>
    /// Image control that obtains the desired image from a resource file
    /// </summary>
    [ToolboxData("<{0}:Image runat=server></{0}:Image>")]
    [Description("Image Control that obtains the desired image from a resource file")]
    [Localizable(true)]
    public class Image : System.Web.UI.WebControls.Image
    {
        /// <summary>
        /// Private member for the ResourceType property
        /// </summary>
        private string _resourceType;
        
        /// <summary>
        /// Private member for the ResouceKeyName property
        /// </summary>
        private string _resourceKeyName;

        /// <summary>
        /// Private member for the ImageType property
        /// </summary>
        private ImageFormat _imageType = ImageFormat.Jpeg;

        /// <summary>
        /// Define the ImageUrl property of the image control from 
        /// registered HTTP handler in web.config file
        /// </summary>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e); 
            
            if (_resourceType != null && _resourceKeyName != null)
            {
                this.ImageUrl = string.Format("MultilanguageControlsHandler.ashx?culture={0}&resource={1}&key={2}&format={3}", Thread.CurrentThread.CurrentUICulture.Name, _resourceType, _resourceKeyName, _imageType.ToString());
            }
        }

        /// <summary>
        /// The type of the desired resource from the App_GlobalResources folder (e.g., for the Images.resx, Images.en-US.resx, Images.es-ES.resx, the type is Resources.Images
        /// </summary>
        [Category("Resources")]
        [Description("The type of the desired resource from the App_GlobalResources folder (e.g., for the Images.resx, Images.en-US.resx, Images.es-ES.resx, the type is Resources.Images")]
        public string ResourceType
        {
            get { return _resourceType; }
            set { _resourceType = value; }
        }

        /// <summary>
        /// The format of the desired image
        /// </summary>
        [Category("Resources")]
        [Description("The format of the desired image")]
        public ImageFormat ImageType
        {
            get { return _imageType; }
            set { _imageType = value; }
        }

        /// <summary>
        /// The name of the resource key of the resource file containing the desired image
        /// </summary>
        [Category("Resources")]
        [Description("The name of the resource key of the resource file containing the desired image")]
        public string ResourceKeyName
        {
            get { return _resourceKeyName; }
            set {_resourceKeyName = value;}
        }

        /// <summary>
        /// This property only take effect at design time. At the run time, ImageUrl is defined from registered HTTP handler in web.config file
        /// </summary>
        [Description("This property only take effect at design time. At the run time, ImageUrl is defined from registered HTTP handler in web.config file")]
        public override string ImageUrl
        {
            get { return base.ImageUrl; }
            set { base.ImageUrl = value; }
        }
    }
}